package com.lvcoding.sessiongxdemo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description 描述
 * @date   2021-12-31 下午3:07
 * @author  wuyanshen
 */
@RequestMapping("test")
@RestController
public class TestController {

    @Value("${server.port}")
    private String port;


    @GetMapping("session")
    public void sessionTest(HttpServletRequest request, HttpServletResponse response) throws IOException {

        System.out.println(port +"被访问了");
        List<String> list = (List<String>)request.getSession().getAttribute("list");

        if (list == null) {
            list = new ArrayList<>();
            // request.getSession().setAttribute("list", list);
        }

        list.add("xxxx");


        // 当session中的数据发生变化时，必须把变化的数据同步到redis中
        request.getSession().setAttribute("list", list);
        response.getWriter().println("size=" + list.size());
        response.getWriter().println("sessionId=" + request.getSession().getId());
        response.getWriter().println("date=" + new Date());
        response.getWriter().println("port=" + port);
    }
}
